package com.youlu.campus.admin.utils;

import java.util.Date;
import java.util.UUID;

/**
 * 表id生成工具
 *
 * @author pdjt
 */
public class IdGenerator {

    public static String generateId() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    /**
     * 商品编号
     * @return
     */
    public static String generateGoodsNo(String prefix){
        StringBuilder sbl = new StringBuilder();
        sbl.append(prefix).append(DateTimeUtils.getDate()).append(getRandNum(1,9999));
        return sbl.toString();
    }

    public static int getRandNum(int min, int max) {
        int randNum = min + (int)(Math.random() * ((max - min) + 1));
        return randNum;
    }


    public static String createOrderNo() {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        return machineId+String.format("%015d", hashCodeV);
    }


    public static String generateYGNo(String date){
        long randomTime = getRandomTime(date);
        StringBuffer sb = new StringBuffer();
        sb.append("YUGOU").append(DateTimeUtils.formatDate(new Date(randomTime),
                "yyyyMMddHHmmss")).append(getRandNum(1,
                99999999));
        return sb.toString();
    }

    public static long getRandomTime(String date) {
        Date strToDate = DateTimeUtils.StringToDate(date, "yyyy/MM/dd");
        String dateToStr = DateTimeUtils.formatDate(strToDate, "yyyyMMdd");
        String startDateToStr = dateToStr +"000000";
        String endDateToStr = dateToStr +"235959";
        return random(DateTimeUtils.StringToDate(startDateToStr, "yyyyMMddHHmmss").getTime(),
                DateTimeUtils.StringToDate(endDateToStr,
                "yyyyMMddHHmmss").getTime());
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }
    
    public static void main(String[] args) {
        System.out.println(generateYGNo("2020/3/16"));

    }


}
